package com.luna.jpa.utils;

import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

public class CriteriaUtils<T> implements Specification<T> {

    private List<CriterionConstant> criterionConstants = new ArrayList<>();

    @Override
    public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
        CriteriaBuilder builder) {
        if (!criterionConstants.isEmpty()) {
            List<Predicate> predicates = new ArrayList<>();
            for (CriterionConstant c : criterionConstants) {
                predicates.add(c.toPredicate(root, query, builder));
            }
            // 将所有条件用 and 联合起来
            if (predicates.size() > 0) {
                return builder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        }
        return builder.conjunction();
    }

    /**
     * 增加简单条件表达式
     *
     * @Methods Name add
     * @Create In 2012-2-8 By lee
     * @param criterionConstant
     */
    public void add(CriterionConstant criterionConstant) {
        if (criterionConstant != null) {
            criterionConstants.add(criterionConstant);
        }
    }
}
